<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/5/31
 * Time: 20:30
 */

namespace Admin\Controller;


use Think\AjaxPage;

class NoticeCategoryController extends BaseController
{
    /**
     * 公告分类列表
     */
    public function ajaxindex(){
        // 搜索条件
        $model = D('NoticeCategory');
        $count = $model->count();
        $Page  = new AjaxPage($count,10);
        //  搜索条件下 分页赋值
//        foreach($condition as $key=>$val) {
//            $Page->parameter[$key]   =   urlencode($val);
//        }
        $sort_order = I('order_by','id').' '.I('sort','desc');

        $categoryList = $model->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('categoryList',$categoryList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function add_category(){
        if(IS_POST){
            $data = I('post.');
            $res = D('NoticeCategory')->add($data);
            if($res){
                $this->success('添加成功',U('NoticeCategory/index'));exit;
            }else{
                $this->error('添加失败',U('NoticeCategory/index'));
            }
        }
        $this->display();
    }

    public function edit_category(){
        if(IS_POST){
            $data = I('post.');
            $res = D('NoticeCategory')->save($data);
            if($res){
                $this->success('保存成功',U('NoticeCategory/index'));exit;
            }else{
                $this->error('保存失败',U('NoticeCategory/index'));
            }
        }else{
            $id = I('id');
            $category = D('NoticeCategory')->find($id);
            $this->assign('category',$category);
        }
        $this->display();
    }

    /**
     * 删除公告类别
     */
    public function delete(){
        $uid = I('get.id');
        $row = M('NoticeCategory')->where(array('id'=>$uid))->delete();
        if($row){
            $this->success('成功删除分类');
        }else{
            $this->error('操作失败');
        }
    }
}